#include "CoreMessageObjectViewer.h"
#include <QMessageBox>
#include <QFileDialog>
#include <QDomDocument>

CoreMessageObjectViewer::CoreMessageObjectViewer(QWidget *parent)
    : QWidget(parent)
{
    setGeometry(100, 100, 1290, 770);
    setMinimumSize(740, 530);
    setMaximumSize(1290, 770);
    QVBoxLayout* mainLayout = new QVBoxLayout(this);
    mainLayout->setMargin(5);
    mainLayout->setSpacing(5);
    setLayout(mainLayout);

    QFrame* menuFrame = new QFrame(this);
    menuFrame->setFrameShape(QFrame::Box);
    menuFrame->setFrameShadow(QFrame::Raised);

    QHBoxLayout* menuLayout = new QHBoxLayout(menuFrame);
    menuLayout->setMargin(5);
    menuLayout->setSpacing(3);

    mFilePathLineEdit = new QLineEdit(menuFrame);
    mFilePathLineEdit->setFocusPolicy(Qt::NoFocus);
    mFilePathLineEdit->setReadOnly(true);

    QPushButton* findFilePushbutton = new QPushButton("FindFile", menuFrame);
    findFilePushbutton->setFocusPolicy(Qt::NoFocus);
    connect(findFilePushbutton, SIGNAL(clicked()), this, SLOT(onClickedFindFile()));

    menuLayout->addWidget(mFilePathLineEdit, 1);
    menuLayout->addWidget(findFilePushbutton, 0);

    mObjectViewer = new ObjectRenderWidget(this);
    mainLayout->addWidget(menuFrame, 0);
    mainLayout->addWidget(mObjectViewer, 1);

    isClear = false;
}

CoreMessageObjectViewer::~CoreMessageObjectViewer()
{
    
}

void CoreMessageObjectViewer::onClickedFindFile(){
    QString filename = QFileDialog::getOpenFileName(this, "Open xml", "./","Object File(*.xml)");
    QFile* file = new QFile(filename);
    if(file->open(QIODevice::ReadOnly)){
        mObjectViewer->clearData();
        mObjectViewer->setData(file->readAll());
        mFilePathLineEdit->setText(filename);
    }
    file->close();
    delete file;
}
